<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="姓名/手机号" prop="user_name">
        <el-input
          v-model="queryParams.user_name"
          placeholder="请输入姓名/手机号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="担保编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入担保编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同号" prop="contract_code">
        <el-input
          v-model="queryParams.contract_code"
          placeholder="请输入合同号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="催款方式" prop="urge_method">
        <el-select
          v-model="queryParams.urge_method"
          placeholder="催款方式"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in urgeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="range">
        <el-date-picker
          v-model="queryParams.range"
          type="daterange"
          value-format="YYYY-MM-DD"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="催收编号" align="center" prop="id" width="160" />
      <el-table-column label="客户姓名" align="center" prop="user_name" />
      <!-- <el-table-column label="担保编号" align="center" prop="apply_id" /> -->
      <el-table-column label="逾期天数" align="center" prop="overdue_days" />
      <el-table-column
        label="逾期本金"
        align="center"
        prop="ctd_principal_amt"
      />
      <el-table-column
        label="逾期利息"
        align="center"
        prop="ctd_interesta_amt"
      />
      <el-table-column label="风险类型" align="center" prop="risk" />
      <el-table-column label="催收方式" align="center">
        <template #default="scope">
          <span>{{
            ["电话催收", "上门/外访", "诉讼"][scope.row.urge_method - 1]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="催收日期"
        align="center"
        prop="urge_time"
        width="160"
      />
      <el-table-column label="催收反馈" align="center" prop="feedback" />
      <el-table-column label="还款金额" align="center" prop="amount" />
      <el-table-column
        label="实际还款日期"
        align="center"
        prop="postSort"
        width="160"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="查看" placement="top">
            <el-button
              link
              icon="View"
              type="primary"
              @click="handelDetail(scope.row)"
              v-hasPermi="['system:post:edit']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page_count"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Expire">
import { methodType, loanTypes } from "@/utils/dict";
import { list } from "@/api/collection/record";

const { proxy } = getCurrentInstance();
const urgeList = ref([
  { label: "电话催收", value: "1" },
  { label: "上门/外访", value: "2" },
  { label: "诉讼", value: "3" },
]);

const router = useRouter();
const postList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    page_count: 10,
    page_size: 1,
    id: "",
    urge_method: "",
    user_name: "",
    contract_code: "",
    range: [],
  },
  rules: {
    postName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  const { range, ...data } = queryParams.value;
  list({
    ...data,
    user_phone: data.user_name,
    start_time: range.length > 0 ? range[0] + " 00:00:00" : "",
    end_time: range.length > 0 ? range[0] + " 23:59:59" : "",
  }).then((response) => {
    postList.value = response.data.list;
    total.value = response.data.total_page;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_count = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 详情 */
function handelDetail(row) {
  console.log(loanTypes(row.loan_type));
  router.push({
    path: "/collection/record-detail",
    state: {
      infos: { ...row },
    },
  });
}
/** 导出按钮操作 */
function handleExport() {
  const { range, ...data } = queryParams.value;
  proxy.download("loan/export/urgePaymentList", {
    ...data,
    user_phone: data.user_name,
    start_time: range.length > 0 ? range[0] + " 00:00:00" : "",
    end_time: range.length > 0 ? range[0] + " 23:59:59" : "",
  });
}

getList();
</script>
